package JouTest;



import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

//import fr.unice.miage.poo.plugins.Plugins;

//import tutoPlugins.plugins.IntPlugins;
import plugins.PluginLoader;
import plugins.Plugins;
//import tutoPlugins.plugins.StringPlugins;

/**
 * Frame principale du programme de test des plugins
 * 
 *
 */
public class MainFrame extends JFrame implements ActionListener{

	
	private static final long serialVersionUID = 4932662545205980307L;
	
	private JMenuBar menuBar;
	private JMenu fileMenu;
	//private JMenu stringPluginsMenu;
	//private JMenu intPluginsMenu;
	
	private JMenuItem exitMenuItem;
	private JMenuItem loadMenuItem;
	private JMenuItem runPluginsMenuItem;
	
	//private JTextArea stringTextArea;
	//private JTextArea intTextArea;
	
	private PluginLoader pluginsLoader;
	private ArrayList files;
	private ArrayList<Plugins> plugins;
	//private ArrayList stringPlugins;
	//private ArrayList intPlugins;
	
	private JMenu pluginsMenu;
	
	public MainFrame(){
		this.pluginsLoader = new PluginLoader();
		this.files = new ArrayList();
		//this.stringPlugins = new ArrayList();
		//this.intPlugins = new ArrayList();
		
		this.initialize();
	}
	
	private void initialize(){
		this.menuBar = new JMenuBar();
		this.fileMenu = new JMenu();
		this.pluginsMenu = new JMenu();
		//this.stringPluginsMenu = new JMenu();
		//this.intPluginsMenu = new JMenu();
		this.exitMenuItem = new JMenuItem();
		this.loadMenuItem = new JMenuItem();
		this.runPluginsMenuItem = new JMenuItem();
		
		//this.stringTextArea = new JTextArea();
		//this.intTextArea = new JTextArea();
		
		//menuBar
		this.menuBar.add(this.fileMenu);
		this.menuBar.add(this.pluginsMenu);
		//this.menuBar.add(this.stringPluginsMenu);
		//this.menuBar.add(this.intPluginsMenu);
		
		//fileMenu
		this.fileMenu.setText("Fichier");
		this.fileMenu.add(this.loadMenuItem);
		this.fileMenu.add(this.runPluginsMenuItem);
		this.fileMenu.addSeparator();
		this.fileMenu.add(this.exitMenuItem);
		
		//stringPluginsMenu
		//this.stringPluginsMenu.setText("Manipulation de String");
		this.pluginsMenu.setText("Plugins charges");
		
		//intPluginsMenu
		//this.intPluginsMenu.setText("Manipulation de int");
		
		//exitMenuItem
		this.exitMenuItem.setText("Fermer");
		this.exitMenuItem.addActionListener(this);
		
		//loadMenuItem
		this.loadMenuItem.setText("Charger un plugins");
		this.loadMenuItem.addActionListener(this);
		
		//runPluginsMenuItem
		this.runPluginsMenuItem.setText("Lancer les plugins charger");
		this.runPluginsMenuItem.addActionListener(this);
		
		//stringTextArea
		/*this.stringTextArea.setBorder(new LineBorder(Color.black));
		this.stringTextArea.setText("Zone pour les plugins sur les String");
		
		//intTextArea
		this.intTextArea.setBorder(new LineBorder(Color.black));
		this.intTextArea.setText("Zone pour les plugins sur les int");
		*/
		
		//this
		this.setSize(800,600);
		this.setJMenuBar(this.menuBar);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new GridLayout(2,1));
		/*this.getContentPane().add(this.stringTextArea);
		this.getContentPane().add(this.intTextArea);*/
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MainFrame().setVisible(true);
	}
	
	
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getSource() == this.exitMenuItem ){
			this.setVisible(false);
		}
		/*
		else {
			if( arg0.getSource() == this.loadMenuItem ){
				JFileChooser f = new JFileChooser(); //ou mettre le nom de dossier où se trouvent les plugins
				
				if(f.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
					this.files.add(f.getSelectedFile().getAbsolutePath());
				}
				
			}
			else {
				if( this.runPluginsMenuItem == arg0.getSource() ){
					this.pluginsLoader.setFiles(this.convertArrayListToArrayString(this.files));
					
					try {
						this.fillPlugins(this.pluginsLoader.loadAllStringPlugins());
					} catch (Exception e) {
						
						e.printStackTrace();
					}
				}//
				else {
					this.ActionFromPlugins(arg0);
				}
			}
		}*/
		
		else {
			if( arg0.getSource() == this.loadMenuItem ){
				JFileChooser f = new JFileChooser(); //ou mettre le nom de dossier où se trouvent les plugins

				if(f.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
					this.files.add(f.getSelectedFile().getAbsolutePath());}
				

			}	//this.files.add(f.getSelectedFile().getAbsolutePath());
				else{
						this.pluginsLoader.setFiles(this.convertArrayListToArrayString(this.files));

						try {
							this.fillPlugins(this.pluginsLoader.loadAllStringPlugins());

						} catch (Exception e) {

							e.printStackTrace();
						}
					
				    }

		}
		
	}
	

	@SuppressWarnings("rawtypes")
	private String[] convertArrayListToArrayString(ArrayList list){
		String[] tmp = new String[list.size()];
		
		for(int index = 0 ; index < tmp.length ; index ++ ){
			tmp[index] = (String)list.get(index);
		}
		
		return tmp;
	}
	
	private void fillPlugins(Plugins[] plugins) {
		// TODO Auto-generated method stub
		JMenuItem menuItem ;

		for(int index = 0 ; index < plugins.length; index ++ ){

			this.plugins.add(plugins[index]);


			menuItem = new JMenuItem();
			menuItem.setText(plugins[index].getLibelle() );
			menuItem.addActionListener(this);
			//Ajout dans la collection de JMenuItem pour d�tection du click
			//this.pluginsMenuItem.add(menuItem);
			//Ajout dans le menu
			boolean textPresent = false;
			for (int j = 0; j < pluginsMenu.getItemCount(); j++) {
				if (pluginsMenu.getItem(j).getText().equals(menuItem.getText())) {
					textPresent = true;
				}
			}
			if (!textPresent) {
				this.pluginsMenu.add(menuItem);
			}

		}
	}


	/*private void fillStringPlugins(StringPlugins[] plugins){
		
		JMenuItem menuItem ;
	
		for(int index = 0 ; index < plugins.length; index ++ ){
			this.stringPlugins.add(plugins[index]);
			
			menuItem = new JMenuItem();
			menuItem.setText(plugins[index].getLibelle() );
			menuItem.addActionListener(this);
			//Ajout dans la collection de JMenuItem pour d�tection du click
			//this.stringPluginsMenuItem.add(menuItem);
			//Ajout dans le menu
			this.stringPluginsMenu.add(menuItem);
		}
		
		
	}*/
	
	private void ActionFromPlugins(ActionEvent e){
		
		for(int index = 0 ; index < this.plugins.size(); index ++ )
		{
			if(e.getActionCommand().equals( ((Plugins)this.plugins.get(index)).getLibelle() )){
				System.out.println(((Plugins)this.plugins.get(index)).getLibelle());

				return ;
			}
		}
		/*for(int index = 0 ; index < this.stringPlugins.size(); index ++ )
		{
			if(e.getActionCommand().equals( ((StringPlugins)this.stringPlugins.get(index)).getLibelle() )){
				this.stringTextArea.setText(((StringPlugins)this.stringPlugins.get(index)).actionOnString(this.stringTextArea.getText()));
				
				return ;
			}
		}
		
		for(int index = 0 ; index < this.intPlugins.size(); index ++ ){
			if(e.getActionCommand().equals( ((IntPlugins)this.intPlugins.get(index)).getLibelle() )){
				
				int res = ((IntPlugins)this.intPlugins.get(index)).actionOnInt( Integer.parseInt(this.stringTextArea.getText()) );
				
				this.stringTextArea.setText( new Integer(res).toString() );
				
				return ;
			}
		}*/
		
		
	}
	
}

